# -*- encoding: utf-8 -*-
"""
File Name: check
Description:
Author : 'lirui'
date: 2024/8/22
Contact: marjey_lee@163.com
Change Activity:
2024/8/22:
"""
import time
from threading import Thread

import requests

import ll
from projects.monitoring.alarm_manager.services.redis_alarm_manager import RedisAlarmManager


class CheckService(Thread):
    def __init__(self):
        super().__init__()
        self._check_mapping = dict(
            branch_dedicated_line_monitor='http://189.12.16.41:903/health_check',
            dedicated_line_monitor='http://189.12.16.41:902/health_check',
            internal_web_spider_monitor='http://189.12.16.46:8080/health_check',
            internet_zabbix_monitoring='http://189.21.16.14:9033/health_check',
            zabbix_monitoring='http://188.12.26.38:900/health_check'
        )

    @staticmethod
    def _send_error():
        """
        Returns:

        """
        content = {'health_check': 0}
        manager = RedisAlarmManager()
        manager.set('monitoring_agent_health_check', content)

    def run(self):
        ll.get_logger().info('start to check agent.')
        while True:
            try:
                self()
            except Exception as e:
                ll.get_logger().error(e)
            time.sleep(10)

    def __call__(self):
        content = {}
        for k, url in self._check_mapping.items():
            try:
                res = requests.get(url)
                if str(res.text) == '1':
                    content[k] = 1
                else:
                    content[k] = 0
            except Exception as e:
                ll.get_logger().error(e)
                content[k] = 0
        manager = RedisAlarmManager()
        manager.set('monitoring_agent_health_check', content)
        k = manager.get('monitoring_agent_health_check')
        print(k)


def _main():
    CheckService()()


if __name__ == '__main__':
    _main()
